<?
/*--|	WORLD
  Software: Socially
   Version: 1.0
   Contact: http://socially.googlecode.com
      Info: http://socially.googlecode.com
   Support: http://socially.googlecode.com
-------------------------------------------------------------------------- 
    Author: goker.cebeci | http://gokercebeci.com
-------------------------------------------------------------------------- 
   License: Distributed under the GNU General Public License (GPL)
            http://www.gnu.org/licenses/gpl.html
 This program is distributed in the hope that it will be useful - WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 FITNESS FOR A PARTICULAR PURPOSE.
-------------------------------------------------------------------------*/
class world {
	const worlddb='data/common/worlddb.json';
	private static function _get(){
            $worlddb = cache::get('worlddb');
            if(!$worlddb){
                $worlddb = json_decode(file_get_contents(self::worlddb));
                cache::set('worlddb', $worlddb, MEMCACHE_COMPRESSED);
            }
            return $worlddb;
	}
	public static function locales(){
            //'lang' is short language code [2 digit]
            return array(
            'index' => array(
                //'ar-sa'=>'ar',
                //'bg-bg'=>'bg',
                //'cs-cz'=>'cs',
                //'da-dk'=>'da',
                //'de-de'=>'de',
                //'el-gr'=>'el',
                'en-gb'=>'en',
                //'en-us'=>'us',
                //'es-es'=>'es',
                //'fi-fi'=>'fi',
                //'fr-fr'=>'fr',
                //'hr-hr'=>'hr',
                //'hu-hu'=>'hu',
                //'it-it'=>'it',
                //'iw-il'=>'he',
                //'ja-jp'=>'ja',
                //'ko-kr'=>'ko',
                //'nl-nl'=>'nl',
                //'nn-no'=>'nn',
                //'pl-pl'=>'pl',
                //'pt-br'=>'br',
                //'pt-pt'=>'pt',
                //'ro-ro'=>'ro',
                //'ru-ru'=>'ru',
                //'sk-sk'=>'sk',
                //'sv-se'=>'sv',
                'tr-tr'=>'tr',
                //'zh-cn'=>'zh',
                //'zh-tw'=>'tw'
                ),
            'locales' => array(
                'ar-sa'=>array('locale'=>'ar_SA','name'=>'Arabic','original'=>'عربي'),
                'bg-bg'=>array('locale'=>'bg_BG','name'=>'Bulgarian','original'=>'Български'),
                'cs-cz'=>array('locale'=>'cs_CZ','name'=>'Czech','original'=>'Čeština'),
                'da-dk'=>array('locale'=>'da_DK','name'=>'Danish','original'=>'Dansk'),
                'de-de'=>array('locale'=>'de_DE','name'=>'German','original'=>'Deutsch'),
                'el-gr'=>array('locale'=>'el_GR','name'=>'Greek','original'=>'Ελληνικά'),
                'en-gb'=>array('locale'=>'en_GB','name'=>'English (UK)','original'=>'English (British)'),
                'en-us'=>array('locale'=>'en_US','name'=>'English (US)','original'=>'English (US)'),
                'es-es'=>array('locale'=>'es_ES','name'=>'Spanish','original'=>'Español'),
                'fi-fi'=>array('locale'=>'fi_FI','name'=>'Finnish','original'=>'suomi'),
                'fr-fr'=>array('locale'=>'fr_FR','name'=>'French','original'=>'Français'),
                'hr-hr'=>array('locale'=>'hr_HR','name'=>'Croatian','original'=>'Hrvatski'),
                'hu-hu'=>array('locale'=>'hu_HU','name'=>'Hungarian','original'=>'Magyar'),
                'it-it'=>array('locale'=>'it_IT','name'=>'Italian','original'=>'Italiano'),
                'iw-il'=>array('locale'=>'iw_IL','name'=>'Hebrew','original'=>'עברית'),
                'ja-jp'=>array('locale'=>'ja_JP','name'=>'Japanese','original'=>'日本語'),
                'ko-kr'=>array('locale'=>'ko_KR','name'=>'Korean','original'=>'한국어'),
                'nl-nl'=>array('locale'=>'nl_NL','name'=>'Dutch','original'=>'Nederlands'),
                'nn-no'=>array('locale'=>'nn_NO','name'=>'Norwegian','original'=>'Norsk nynorsk'),
                'pl-pl'=>array('locale'=>'pl_PL','name'=>'Polish','original'=>'Polski'),
                'pt-br'=>array('locale'=>'pt_BR','name'=>'Portuguese (Brazilian)','original'=>'Português (do Brasil)'),
                'pt-pt'=>array('locale'=>'pt_PT','name'=>'Portuguese','original'=>'Português'),
                'ro-ro'=>array('locale'=>'ro_RO','name'=>'Romanian','original'=>'română'),
                'ru-ru'=>array('locale'=>'ru_RU','name'=>'Russian','original'=>'Русский'),
                'sk-sk'=>array('locale'=>'sk_SK','name'=>'Slovak','original'=>'slovenčina'),
                'sv-se'=>array('locale'=>'sv_SE','name'=>'Swedish','original'=>'Svenska'),
                'tr-tr'=>array('locale'=>'tr_TR','name'=>'Turkish','original'=>'Türkçe'),
                'zh-cn'=>array('locale'=>'zh_CN','name'=>'Chinese (China)','original'=>'中文 (简体)'),
                'zh-tw'=>array('locale'=>'zh_TW','name'=>'Chinese (Taiwan)','original'=>'正體中文 (繁體)')
                )
            );
	}
	public static function locale($locale,$lang){
            $locale = strtolower($locale);
            $locales = self::locales();
            $deflocale = array_search(DEFLANG, $locales['index']);
            if($lang)
                $locale = array_search($lang, $locales['index']);
            return $locales['index'][$locale]
                   ? array($locales['locales'][$locale]['locale'],$locales['index'][$locale])
                   : array($locales['locales'][$deflocale]['locale'],$locales['index'][$deflocale]);
	}
        public static function languages(){
            $locales = self::locales();
            foreach($locales['index'] as $locale=>$code) $languages[$code] = $locales['locales'][$locale]['original'];
            return $languages;
        }
	public static function countries($locale){
            $countries = cache::get("countries[$locale]");
            if(!$countries){
                $world = self::_get();
		$defaultLocale = $locale;
		$countries = new stdClass();
		foreach ($world->country as $code=>$country){
                    $countries->$code = $country->name->$locale
                                        ? $country->name->$locale
                                        : $country->name->$defaultLocale;
		}
                cache::set("countries[$locale]", $countries);
            }
            return $countries;
	}
	public static function regions($country, $locale){
            if(!$country) return false;
            $regions = cache::get("regions[$country][$locale]");
            if(!$regions){
                $world = self::_get();
                $defaultLocale = $locale;
                $regions = new stdClass();
                foreach ($world->country->$country->region as $code=>$region){
                        $regions->$code = $region->name->$locale
                                          ? $region->name->$locale
                                          : $region->name->$defaultLocale;
                }
                cache::set("regions[$country][$locale]", $regions);
            }
            return $regions;
	}
	public static function country($country, $locale){
            if(!$country) return false;
            $c = cache::get("country[$country][$locale]");
            if(!$c){
                $world = self::_get();
                $defaultLocale = $locale;
                $c = $world->country->$country->name->$locale
                                ? $world->country->$country->name->$locale
                                : $world->country->$country->name->$defaultLocale;
                cache::set("country[$country][$locale]", $c);
            }
            return $c;
	}
	public static function region($country, $region, $locale){
            if(!$country || !$region) return false;
            //echo $region = substr($region, 1);
            $r = cache::get("region[$country][$region][$locale]");
            if(!$r){
                $world = self::_get();
                $defaultLocale = $locale;
                $r = $world->country->$country->region->$region->name->$locale
                                ? $world->country->$country->region->$region->name->$locale
                                : $world->country->$country->region->$region->name->$defaultLocale;
                cache::set("region[$country][$region][$locale]", $r);
            }
            return $r;
	}
        public static function ip($IP){
            $country = @geoip_country_code_by_name($IP);
            //$timezone = @geoip_time_zone_by_country_and_region($country, $region);
            $r = @geoip_record_by_name($IP);
            return array(
                'country'=> $country,
                'region'=> $r['region'],
                //'city'=> $r['city'],
                'latitude'=> $r['latitude'],
                'longitude'=> $r['longitude'],
                'timezone'=> $timezone
            );
        }
}
?>